package cn.jiyun.service;

import cn.jiyun.entity.ResultClass;
import cn.jiyun.entity.day11.TestPaper;
import cn.jiyun.entity.day11.TestPaperDTO;
import cn.jiyun.entity.day11.TestPaperVO;
import cn.jiyun.entity.day11.UserTestPaperOver;
import cn.jiyun.entity.day15.TrainDTO;
import cn.jiyun.entity.day9.CourseVO;
import cn.jiyun.service.fegin.CourseFeginService;
import cn.jiyun.service.fegin.DirectoryFeginService;
import cn.jiyun.service.fegin.TestPaperFeginService;
import cn.jiyun.service.fegin.TrainFeginService;
import cn.jiyun.utils.PageUtils;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author sjt
 * @Date 2023/8/31 11:42
 * @PackageName: cn.jiyun.service.fegin.EightDayService
 */
@Service
public class DayNineService {
    @Autowired
    TrainFeginService trainFeginService;
    @Autowired
    CourseFeginService courseFeginService;
    @Autowired
    TestPaperFeginService testPaperFeginService;
    @Autowired
    PageUtils pageUtils;
    @Autowired
    RedisTemplate redisTemplate;

    //分页查询试卷的集合
    public ResultClass findTestPaperList(TestPaperDTO dto) {
        List<TestPaperVO> list=testPaperFeginService.findTestPaperList(dto);
        list.forEach(testPaperVO -> {
            //查询试题数
            Integer testPaperTopicNum = testPaperFeginService.findTestPaperTopicNum(testPaperVO.getId());
            testPaperVO.setTopicNum(testPaperTopicNum);
            //查询完成人数
            Integer overNum = testPaperFeginService.selectTestPaperOverNum(testPaperVO.getId());
            testPaperVO.setOverNum(overNum);
        });
//        如果是查询最热的(完成人数最多的)
//        根据完成人数排序
        if (StringUtil.isNotEmpty(dto.getStatus()) && dto.getStatus().equals("3")){
            Collections.sort(list, Comparator.comparingInt(TestPaperVO::getOverNum));
        }
//        设置分页规则
        PageInfo pageInfo = pageUtils.pageHaper(list, dto.getPageNum(), dto.getPageSize());
        return ResultClass.ok(pageInfo);
    }
    //提交模拟考试记录
    public ResultClass setUserTestPaperOver(UserTestPaperOver over) {
        over.setOverDate(new Date());
        Integer oid = testPaperFeginService.setUserTestPaperOver(over);
        if(oid!=null){
            return ResultClass.ok(oid).Message("试卷提交成功");
        }
        return ResultClass.error().Message("试卷提交失败");
    }
}
